package com.wendy.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wendy.admin.domain.bo.SysOperLogBo;
import com.wendy.admin.domain.vo.SysLogininforExcelVo;
import com.wendy.admin.domain.vo.SysOperLogExcelVo;
import com.wendy.admin.domain.vo.SysOperLogVo;
import com.wendy.admin.entity.SysOperLog;
import com.wendy.admin.service.ISysOperLogService;
import com.wendy.common.core.domain.TableDataInfo;
import com.wendy.common.log.annotation.Log;
import com.wendy.common.log.enums.BusinessType;
import com.wendy.common.web.controller.BaseController;
import com.wendy.custom.security.annotation.InnerAuth;
import com.wendy.custom.security.annotation.RequiresPermissions;
import com.wendy.excel.ruoyi.utils.ExcelUtil;
import com.wendy.utils.spring.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 操作日志记录
 *
 * @author wendy
 */
@RestController
@RequestMapping("/operlog")
public class SysOperlogController extends BaseController {
    @Autowired
    private ISysOperLogService operLogService;

    @RequiresPermissions("system:operlog:list")
    @GetMapping("/page")
    public TableDataInfo page(SysOperLogBo operLog) {
        TableDataInfo<SysOperLogVo> result = operLogService.selectOperLogPage(operLog);
        return result;
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLogBo operLog) {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        List<SysOperLogExcelVo> rlist = MapstructUtils.convert(list, SysOperLogExcelVo.class);
        ExcelUtil<SysOperLogExcelVo> util = new ExcelUtil<SysOperLogExcelVo>(SysOperLogExcelVo.class);
        util.exportExcel(response, rlist, "操作日志");
    }

    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:operlog:remove")
    @DeleteMapping("/{operIds}")
    public int remove(@PathVariable Long[] operIds) {
        return operLogService.deleteOperLogByIds(operIds);
    }

    @RequiresPermissions("system:operlog:remove")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Boolean clean() {
        operLogService.cleanOperLog();
        return true;
    }

    @InnerAuth
    @PostMapping
    public boolean add(@RequestBody SysOperLog operLog) {
        int i = operLogService.insertOperlog(operLog);
        return i > 0;
    }
}
